package com.office.supplies.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.supplies.mapper.EmployeeMapper;
import com.office.supplies.pojo.Employee;
import com.office.supplies.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/09/19:57
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工注册
     *
     * @param employee Employee
     */
    public void registerEmployee(Employee employee) {
        // md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        employeeMapper.insert(employee);
    }

    /**
     * 员工登录
     *
     * @param employee Employee
     * @return Employee
     */
    public Employee login(Employee employee) {
        // md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        Employee emp = employeeMapper.selectEmployee(employee);
        if (emp == null) {
            throw new RuntimeException("用户名或密码或角色错误!");
        }
        return emp;
    }

    /**
     * 根据id获取员工信息
     *
     * @param id Long
     * @return Employee
     */
    public Employee getEmployeeById(Long id) {
        return employeeMapper.selectEmployeeById(id);
    }

    /**
     * 条件分页查询员工信息
     *
     * @param pageNum  Integer
     * @param pageSize Integer
     * @param employee Employee
     * @return Page<Employee>
     */
    public PageInfo<Employee> getAllEmployees(Integer pageNum, Integer pageSize, Employee employee) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employees = employeeMapper.selectAllEmployee(employee);
        return new PageInfo<>(employees);
    }

    /**
     * 修改员工
     *
     * @param employee Employee
     */
    public void updateEmployee(Employee employee) {
        // md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        employeeMapper.updateEmployee(employee);
    }

    /**
     * 删除员工
     *
     * @param id Long
     */
    public void deleteEmpLoyee(Long id) {
        employeeMapper.deleteEmployeeById(id);
    }

}
